<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Employee_Leaves extends CI_Controller
{
	function __construct()
	{
		parent::__construct();		
		if (!$this->tank_auth->is_logged_in()) redirect('auth/login');				
		
		//load models
		$this->load->model('Employee_Leaves_db');
		$this->load->model('employees/Employees_db');
		$this->load->model('leaves/Leaves_db');
	}
	
	/*VIEW FUNCTIONS*/
	function index()
	{	
		redirect('employee_leaves/manage');
	}	
	
	function manage()
	{
		//default
		$data['ptitle'] = "Manage Employee Leaves";				
		$data['username'] = $this->tank_auth->get_username();
		$data['module'] = 'employee_leaves';
		$data['active_header'] = 'admin';
		//end default		
		
		$data['content'] = $this->load->view('manage',$data,true);		
		$this->load->view('default_view',$data);
	}
	
	function edit($id)
	{		
		//default
		$data['ptitle'] = "Edit Employee Leaves";				
		$data['username'] = $this->tank_auth->get_username();
		$data['module'] = 'employee_leaves';
		$data['active_header'] = 'admin';
		//end default
		
		//dropdowns
		$data['leaves'] = $this->Employee_Leaves_db->getAvailableLeaves($id);
		
		//check shift if existing
		$emp = $this->Employees_db->getEmployee(array('id'=>$id));
			if(!count($emp)) show_404('page');
		
		$data['item'] = $id;
		$data['content'] = $this->load->view('edit',$data,true);		
		$this->load->view('default_view',$data);
	}
	/*END VIEW FUNCTIONS*/
	
	
	/* AJAX FUNCTIONS */
	
	function addItem()
	{
		$dt = null;
		$error = false;
		foreach($_POST as $name => $value) {
			if (isset($_POST[$name])) $dt[$name] = $this->input->post($name);
		}
		
		//set additional data
		$now = date('Y-m-d H:i:s');
		$dt['created'] = $now;
		$dt['created_by'] = $dt['modified_by'] = $this->tank_auth->get_user_id();	
		
		$leaves = $this->Leaves_db->getLeaves(array('leave_id'=>$dt['leave_id']));
		if(count($leaves))
			$dt['available_days'] = $leaves[0]['no_of_days'];
		
		$item = $this->Employee_Leaves_db->save($dt);
		
		//audit trail
		$this->Audittrail_db->save(array('module'=>'employee_leaves', 'item_id'=>$item, 'action'=>'add', 'description'=>serialize($dt), 'created'=>$now, 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	function viewItem()
	{
		$dt['id'] = $this->input->post('item');
		$details = $this->Employees_db->getEmployee($dt);
		if ($details)
		{
			echo json_encode($details);
		}
	}	
	
	function updateItem()
	{	
		$id = $this->input->post('item');		
		$dt = null;
		$error = false;
		foreach($_POST as $name => $value) {
			if($name!='item')
				if (isset($_POST[$name])) $dt[$name] = $this->input->post($name);
		}		
					
		//set additional data
		$dt['modified'] = date('Y-m-d H:i:s');
		$dt['modified_by'] = $this->tank_auth->get_user_id();		
		$item = $this->Employee_Leaves_db->save($dt, $id);
		
		
		//audit trail
		$this->Audittrail_db->save(array('module'=>'employee_leaves', 'item_id'=>$item, 'action'=>'update', 'description'=>serialize($dt), 'created'=>$dt['modified'], 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	function deleteItem()
	{
		$dt = $this->input->post('items');
		$item = $this->Employee_Leaves_db->delete($dt);
		
		//audit trail
		foreach($dt as $d)
			$this->Audittrail_db->save(array('module'=>'employee_leaves', 'item_id'=>$d, 'action'=>'delete', 'description'=>serialize($dt), 'created'=>date('Y-m-d H:i:s'), 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
		
	
	function listview()
	{
		$data = $this->Employee_Leaves_db->getList();
		echo json_encode($data);
	}
	
	function listviewemployeeleaves($userid)
	{
		$data = $this->Employee_Leaves_db->getEmployeeLeavesList($userid);
		echo json_encode($data);
	}
	
	function getAvailableLeaves()
	{
		$data = $this->Employee_Leaves_db->getAvailableLeaves($this->input->post('user_id'));
		echo json_encode($data);
	}
	
	/* END AJAX FUNCTIONS */
	
	
	
	/* LOAD MODULE JS */
	function js() {
        $this->load->view('js/script.js');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */